﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace _00_数据保存
{
    public class INI
    {
        /// <summary>
        /// 写入INI配置文件
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        public static extern bool WritePrivateProfileString(string section, string key, string value, string filePath);

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="section">根节点</param>
        /// <param name="key">键</param>
        /// <param name="StrDefault">找不到的情况下，返回的默认字符串</param>
        /// <param name="retval">返回的字符串[值]</param>
        /// <param name="size">返回的字符串的大小</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        public static extern int GetPrivateProfileString(string section, string key, string StrDefault, StringBuilder retval, int size, string filePath);

        /// <summary>
        /// 自定义读取INI文件
        /// </summary>
        /// <param name="Section">根节点名称</param>
        /// <param name="Key">键</param>
        /// <param name="strFilePath">文件路径</param>
        /// <returns>值</returns>
        public static string ContentValue(string Section, string Key, string strFilePath)
        {
            int size = 1024;
            StringBuilder stringBuilder = new StringBuilder(size);
            GetPrivateProfileString(Section, Key, "", stringBuilder, size, strFilePath);
            return stringBuilder.ToString();

        }

    }
}
